/* 
source: 即将被赋值的对象
target: 即将赋值给的对象
return: source
*/
type TCopyDataItem = {
  [key in string]: string
}
type TCopyData = (source: TCopyDataItem, target: TCopyDataItem) => TCopyDataItem
export const copyData: TCopyData = (source, target) => {
  for (const key in target) {
    source[key] !== undefined && (source[key] = target[key])
  }
  return source
}
function add0(m: number) {
  return m < 10 ? '0' + m : m
}
export function formatTime(Time: string) {
  //shijianchuo是整数，否则要parseInt转换
  var time = new Date(parseInt(Time))
  var y = time.getFullYear()
  var m = time.getMonth() + 1
  var d = time.getDate()
  var h = time.getHours()
  var mm = time.getMinutes()
  var s = time.getSeconds()
  return y + '-' + add0(m) + '-' + add0(d) + '  ' + add0(h) + ':' + add0(mm) + ':' + add0(s)
}

const jieliu = () => {
  let timer: NodeJS.Timeout | null = null
  return (cb: Function, time: number) => {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      cb()
    }, time)
  }
}
const jieliuUtil = jieliu()
const calcCount = () => {
  let currentCount = 0
  return (cb: Function, count: number) => {
    currentCount++
    if (currentCount >= count) {
      cb()
      currentCount = 0
    }
  }
}
const calcCountUtil = calcCount()
const limitFn = () => {
  const limit = {
    flag: true
  }
  return (cb: Function) => {
    if (limit.flag) {
      limit.flag = false
      cb(limit)
    }
  }
}
const limitFnUtil = limitFn()
export { calcCountUtil }
export { limitFnUtil }
export { jieliuUtil }
